<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_permint_pem_desain_promo extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_permint_pem_desain_promo_model', 'PemPromo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Permintaan Pembuatan Desain Promo";
		viewByTemplate('pro_permint_pem_desain_promo/index', $data, 'grid');
	}
	
	
	function getdata()
	{
		$this->secure_session();
		echo $this->PemPromo->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->PemPromo->GetJSON2();
	}

		
	function create()
	{
		// Display Form Create
		$data['title'] = "New Data Permintaan Desain Promosi";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";

		$query = $this->db->query("SELECT kode_promosi FROM (SELECT * FROM pro_desain_promo ORDER BY kode_promosi DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_promosi'];
		else
			$MaxID = "PR0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$query1 = $this->db->query("SELECT id_desain FROM (SELECT * FROM pro_desain_promo ORDER BY id_desain DESC) WHERE rownum <=1");
	 				if ($query1->row_array()) {
	 					$getquery1 = $query1->row_array();
	 					$rowcount1 = $getquery1['id_desain'];
	 					$autonum1 = $rowcount1 + 1;
	 				} else {
					$autonum1 = 1;
				}
		$data['id_desain_auto'] = $autonum1;
		//echo json_encode($data);
	
		$data['kode_promosi'] = "PR" . sprintf("%07s",$NextID);
		viewByTemplate('pro_permint_pem_desain_promo/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Permintaan Desain Promosi";
		$data['data'] = $this->PemPromo->GetSingleView(get('kode_promosi'));
		$data['id_desain_edit'] = get('id_desain');
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('pro_permint_pem_desain_promo/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Master";
		$data['data'] = $this->PemPromo->GetSingleView(get('kode_promosi'));
		
		$data['mode'] = "view";
		viewByTemplate('pro_permint_pem_desain_promo/form', $data, 'form');
	}
	
	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		if (post("kode_promosi") == '') {
			$error->Add("kode_promosi", "Kode Promosi", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM pro_desain_promo WHERE kode_promosi = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kode_promosi"))))."'")) > 0) {
			$error->Add("kode_promosi", "Kode Promosi", $tpl_duplicate);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		// Get Error Template
		$tpl_insert_success = $this->MessageTemplate("CS001");
		$tpl_update_success = $this->MessageTemplate("CS002");
		

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= array('kode_promosi' => $this->input->post('kode_promosi')
								, 'id_desain' => $this->input->post('id_desain')
								, 'tanggal_permintaan' => $this->input->post('tanggal_permintaan')
								, 'id_departemen_pk' => $this->input->post('id_departemen_pk')
								, 'nama_departemen' => $this->input->post('nama_departemen')
								, 'status_permintaan' => $this->input->post('status_permintaan')
								, 'nama_desain' => $this->input->post('nama_desain')
								, 'jenis_desain' => $this->input->post('jenis_desain')
								, 'keterangan' => $this->input->post('keterangan') );
				//$data['kode_promosi']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['kode_promosi'])));

				$arr_tgl_perm = explode('/', $data['tanggal_permintaan']);
				$tanggal_permintaan = $arr_tgl_perm[2].'-'.sprintf("%02s",$arr_tgl_perm[1]).'-'.sprintf("%02s",$arr_tgl_perm[0]);
				
				$data['tanggal_permintaan'] = $tanggal_permintaan;
				// Set Primary Key Variable
				$kode_promo	= $data['kode_promosi'];


				if (post("mode") == "create")
				{
					$this->PemPromo->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->PemPromo->Update($data, array('kode_promosi'	=> $data['kode_promosi']));

					$ls_response = "Data Tersimpan [edited]";
				}

			// 	// ----- >> Detail Sample 2
			// 	$dtl_sample2	= $this->data['dtl_sample2'];
			// 	$total_deleted	= $dtl_sample2['num_row']['deleted'];
			// 	$total_inserted	= $dtl_sample2['num_row']['inserted'];
			// 	$total_updated	= $dtl_sample2['num_row']['updated'];
				
			// 	for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
			// 		$row = $dtl_sample2['deleted'][$i];					
			// 		$this->Detail->DeleteSingle($row['kode_desain']);
			// 	}
				
				
			// 	for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					
			// 		// Auto number kode detail penjualan
					
					
			// 		$query = $this->db->query("SELECT no_detail FROM (SELECT * FROM pro_desain_promo2 ORDER BY no_detail DESC) WHERE rownum <= 1");
			// 		$getquery = $query->row_array();
			// 		$rowcount = $getquery['no_detail'];
			// 		$autonum = $rowcount + 1;

			// 		$row = $dtl_sample2['inserted'][$i];
			// 		$row['no_detail'] = $autonum;
			// 		$row['kode_promo'] = $kode_promo;
			// 		$row['kode_desain'] = $row['kode_desain'];
			// 		$row['nama_desain'] = $row['nama_desain'];
			// 		$row['jenis_desain'] = $row['jenis_desain'];
			// 		$this->Detail->Add($row);
			// 	}
				
			// 	for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
			// 		$row = $dtl_sample2['updated'][$i];					
			// 		$kode_desain	= $row['kode_desain'];
			// 		unset($row['kode_desain']);					
			// 		//$row['jumlah'] = (float)$row['jumlah'];					
			// 		$this->Detail->Update($row, array('kode_desain'	=> $kode_desain));
			// 	}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kode_promo	= get('kode_promosi');
		
		if ($kode_promo != "")
		{
			// Delete Single
			$arr_rule	= array('kode_promosi' => $kode_promo);
			$ls_result = $this->PemPromo->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kode_promosi' => $row['kode_promosi']);
				$ls_result .= $this->PemPromo->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>